﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using UI.BudgetManage;
using BL.Client.BudgetManageBL;

namespace UI.BudgetManage.ChildForm.InformationManage
{　　
    public partial class InformationManage : Form
    {
        BudgetManageServerice BMS;

        public InformationManage()
        {
            InitializeComponent();

        }

        private void InformationManage_Load(object sender, EventArgs e)
        {
            this.BMS = ((BMMain)this.MdiParent).BMS;

            this.tbxParent.ReadOnly = true;

            this.btnSubmit.Enabled = false;
            this.btnDelete.Enabled = false;
            this.btnCancel.Enabled = false;
            this.btnModify.Enabled = false;

            this.ShowTreeviewData();
        }

        private void ShowTreeviewData()
        {
            DataSet ds_p, ds_c;
            tvBudgetType.Nodes.Clear();
            tvBudgetType.Nodes.Add(null, "所有科目");
            TreeNode RootNode = tvBudgetType.Nodes[0];
            ds_p = this.BMS.IBC.GetBudgetType();
            if (ds_p == null)
            {
                MessageBox.Show("请检查网络连接！");
                return;
            }
            else
            {
                for (int i = 0; i < ds_p.Tables[0].Rows.Count; i++)
                {
                    string key = ds_p.Tables[0].Rows[i]["budget_type_id"].ToString();
                    string text = ds_p.Tables[0].Rows[i]["budget_type_name"].ToString();
                    RootNode.Nodes.Add(key, text);
                    ds_c = this.BMS.IBC.GetBudgetSubType(key);
                    for (int j = 0; j < ds_c.Tables[0].Rows.Count; j++)
                    {
                        key = ds_c.Tables[0].Rows[j]["budget_type_id"].ToString();
                        text = ds_c.Tables[0].Rows[j]["budget_type_name"].ToString();
                        RootNode.Nodes[i].Nodes.Add(key, text);
                    }
                }
                tvBudgetType.ExpandAll();
            }
        }

        private void f()
        {
            
        }

        private void btnAddBudgetPType_Click(object sender, EventArgs e)
        {
            DataSet ds_p, ds_c;
            int budget_type_id = 0;

            ds_p = this.BMS.IBC.GetBudgetType();

            if (ds_p == null)
            {
                MessageBox.Show("请检查网络连接！");
                return;
            }
            else
            {
                for (int i = 0; i < ds_p.Tables[0].Rows.Count; i++)
                {
                    string key = ds_p.Tables[0].Rows[i]["budget_type_id"].ToString();
                    if( budget_type_id <Convert.ToInt32(key))
                        budget_type_id =Convert.ToInt32(key);

                    ds_c = this.BMS.IBC.GetBudgetSubType(key);
                    for (int j = 0; j < ds_c.Tables[0].Rows.Count; j++)
                    {
                        key = ds_c.Tables[0].Rows[j]["budget_type_id"].ToString();
                        if (budget_type_id < Convert.ToInt32(key))
                            budget_type_id = Convert.ToInt32(key);
                    }
                }
                string addId = (budget_type_id+1).ToString();
                string addName = "Node" + (budget_type_id+1).ToString();

                TreeNode node=new TreeNode();
                node.Name=addId;
                node.Text=addName;
                TreeNode RootNode = tvBudgetType.Nodes[0];
                RootNode.Nodes.Add(node);
                tvBudgetType.ExpandAll();

                this.tvBudgetType.SelectedNode=node;
               // this.tvBudgetType.SelectedNode.BackColor = System.Drawing.Color.SkyBlue;

                this.tbxId.Text = addId;
                this.tbxName.Text = addName;
                this.tbxParent.Text = "";

                this.addBudgetType();

                this.btnAddBudgetPType.Enabled = false;
                this.btnAddBudgetSType.Enabled = false;
                this.btnDelete.Enabled = false;
                this.btnSubmit.Enabled = true;
                this.btnCancel.Enabled = true;
                this.btnCancel.Click += new System.EventHandler(this.btnCancelAddParent_Click);                
            }
        }

        private void btnCancelAddParent_Click(object sender, EventArgs e)
        {
            this.tvBudgetType.SelectedNode.Remove();
            this.tbxId.Text = "";
            this.tbxName.Text = "";

            this.btnSubmit.Enabled = false;
            this.btnCancel.Enabled = false;
        }

        private void btnAddBudgetSType_Click(object sender, EventArgs e)
        {
            try
            { 
                if (this.tvBudgetType.SelectedNode.Name =="")
                {
                    MessageBox.Show("请选择需要添加二级科目的一级科目！");
                    return;
                }
                else if (this.tvBudgetType.SelectedNode.Parent.Name != "")
                {
                    this.tvBudgetType.SelectedNode = this.tvBudgetType.SelectedNode.Parent;
                }

                this.addBudgetSType();

                this.btnAddBudgetPType.Enabled = false;
                this.btnAddBudgetSType.Enabled = false;
                this.btnSubmit.Enabled = true;
                this.btnDelete.Enabled = false;
                this.btnModify.Enabled = false;
                return;

            }
            catch(Exception ex)
            {
                MessageBox.Show("服务器繁忙，稍后再试！");
                return;
            }
        }

        private void addBudgetSType()
        { 
            DataSet ds_p, ds_c;
            int budget_type_id = 0;

            ds_p = this.BMS.IBC.GetBudgetType();

            if (ds_p == null)
            {
                MessageBox.Show("请检查网络连接！");
                return;
            }
            else
            {
                for (int i = 0; i < ds_p.Tables[0].Rows.Count; i++)
                {
                    string key = ds_p.Tables[0].Rows[i]["budget_type_id"].ToString();
                    if (budget_type_id < Convert.ToInt32(key))
                        budget_type_id = Convert.ToInt32(key);

                    ds_c = this.BMS.IBC.GetBudgetSubType(key);
                    for (int j = 0; j < ds_c.Tables[0].Rows.Count; j++)
                    {
                        key = ds_c.Tables[0].Rows[j]["budget_type_id"].ToString();
                        if (budget_type_id < Convert.ToInt32(key))
                            budget_type_id = Convert.ToInt32(key);
                    }
                }
                string addId = (budget_type_id + 1).ToString();
                string addName = "Node" + (budget_type_id + 1).ToString();

                TreeNode node = new TreeNode();
                node.Name = addId;
                node.Text = addName;

                this.tvBudgetType.SelectedNode.Nodes.Add(node);

                this.tbxId.Text = addId;
                this.tbxName.Text = addName;
                this.tbxParent.Text = this.tvBudgetType.SelectedNode.Text;

                this.tvBudgetType.SelectedNode = node;

                this.addBudgetType();

                this.btnCancel.Click += new System.EventHandler(this.btnCancelAddChild_Click);
                return;
            }
        }

        private void btnCancelAddChild_Click(object sender, EventArgs e)
        {
            this.tvBudgetType.SelectedNode.Remove();
            this.tbxId.Text = "";
            this.tbxName.Text = "";
            this.tbxParent.Text = "";

            this.btnSubmit.Enabled = false;
            this.btnCancel.Enabled = false;        
        }

        private void addBudgetType()
        {
            DataSet ds;
            int i = 0;

            if (this.tbxId.Text.ToString()!="")
            {   
                if (this.tbxName.ToString()!= "")
                {
                    try
                    {
                        ds = this.BMS.IIM.SelectBudgetType_by_Budget_id(tbxId.Text.ToString());

                        i = ds.Tables[0].Rows.Count;

                        if (i < 1)
                        {
                            if (this.tbxParent.Text.ToString() =="")
                                this.BMS.IIM.AddBudgetType(tbxId.Text.ToString(), tbxName.Text.ToString(), null);
                            else
                                this.BMS.IIM.AddBudgetType(tbxId.Text.ToString(), tbxName.Text.ToString(),this.tvBudgetType.SelectedNode.Parent.Name);

                            this.tvBudgetType.ExpandAll();
                            this.tvBudgetType.Refresh();
                            
                            MessageBox.Show("添加科目成功！");
                            this.btnAddBudgetPType.Enabled = true;
                            this.btnAddBudgetSType.Enabled = true;

                            this.tvBudgetType.SelectedNode.Text = this.tbxName.Text;

                            return;
                        }
                        else
                        {
                            MessageBox.Show("科目编号为" + this.tbxId.Text + "的信息已存在，请重新编号！");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }
                }
                else
                    MessageBox.Show("科目编号不能为空！");
                return;
            }
            else
                MessageBox.Show("科目名称不能为空！");
            return;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int i = 0;
            bool deleteSuccess=false;
            this.btnSubmit.Enabled = false;
            this.btnModify.Enabled = false;

            try
            {
                if (this.tvBudgetType.SelectedNode.Name != "")
                {
                    i = this.tvBudgetType.SelectedNode.Nodes.Count;
                    for (int j = i; j > 0; j--)
                        this.BMS.IIM.DeleteBudgetType_by_parentid(this.tvBudgetType.SelectedNode.Name);

                    deleteSuccess = this.BMS.IIM.DeleteBudgetType_by_Budget_id(this.tvBudgetType.SelectedNode.Name);

                    if (deleteSuccess == true)
                    {
                        this.tvBudgetType.SelectedNode.Remove();
                        this.tbxId.Text = "";
                        this.tbxName.Text = "";
                        this.tbxParent.Text = "";

                        // this.tvBudgetType.ExpandAll();
                        MessageBox.Show("删除成功！");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("请选择需要删除的一级或二级科目！");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器繁忙，请稍后再试！");
                return;
            }
        }

        private void tbxIdTextChanged_Click(object sender, EventArgs e)
        {  
            if(this.tvBudgetType.SelectedNode.Name!="")
              this.btnModify.Enabled = true;
        }

        private void tbxNameTextChanged_Click(object sender, EventArgs e)
        {
            if (this.tvBudgetType.SelectedNode.Name != "")
            this.btnModify.Enabled = true;
        }

        private void tvBudgetType_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DataSet ds;
            int i = 0;

            this.btnAddBudgetPType.Enabled = true;
            this.btnAddBudgetSType.Enabled = true;
            this.btnSubmit.Enabled = false;
            this.btnModify.Enabled = false;
            this.btnDelete.Enabled = true;
            this.btnCancel.Enabled = true;

            try
            {
                if (this.tvBudgetType.SelectedNode.Name !="")
                {
                    ds = this.BMS.IIM.SelectBudgetType_by_Budget_id(this.tvBudgetType.SelectedNode.Name);
                    i = ds.Tables[0].Rows.Count;

                    if (i > 0)
                    {
                        this.tbxId.Text = ds.Tables[0].Rows[0]["budget_type_id"].ToString();
                        this.tbxName.Text = ds.Tables[0].Rows[0]["budget_type_name"].ToString();
                        if (ds.Tables[0].Rows[0]["budget_type_parentid"].ToString() != "")
                            this.tbxParent.Text = this.tvBudgetType.SelectedNode.Parent.Text;
                        else
                            this.tbxParent.Text = "";

                        this.btnModify.Enabled = false;
                        //modify 是否可修改
                        this.tbxId.TextChanged += new System.EventHandler(this.tbxIdTextChanged_Click);
                        this.tbxName.TextChanged += new System.EventHandler(this.tbxNameTextChanged_Click);
                    }
                    return;
                }
                else
                {
                    this.tbxId.Text = this.tvBudgetType.Nodes[0].Name;
                    this.tbxName.Text = this.tvBudgetType.Nodes[0].Text;
                    this.tbxParent.Text = "";

                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器繁忙，请稍后再试！" + ex.ToString());
                return;
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            DataSet ds;
            int i = 0;

            if (this.tbxId.Text.ToString() != "")
            {
                if (this.tbxName.ToString() != "")
                {
                    try
                    {
                        ds = this.BMS.IIM.SelectBudgetType_by_Budget_id(tbxId.Text.ToString());

                        i = ds.Tables[0].Rows.Count;

                        if (i < 1)
                        {
                            this.BMS.IIM.UpdateBudgetType(tbxId.Text.ToString(),tbxName.Text.ToString(),this.tvBudgetType.SelectedNode.Name);
 
                            this.tvBudgetType.ExpandAll();

                            MessageBox.Show("修改科目成功！");
                            this.btnAddBudgetPType.Enabled = true;
                            this.btnAddBudgetSType.Enabled = true;

                            this.tvBudgetType.SelectedNode.Text = this.tbxName.Text;

                            return;
                        }
                        else
                        {
                            MessageBox.Show("科目编号为" + this.tbxId.Text + "的信息已存在，请重新编号！");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        return;
                    }
                }
                else
                    MessageBox.Show("科目编号不能为空！");
                return;
            }
            else
                MessageBox.Show("科目名称不能为空！");
            return;
        }
    }

    //private void cbbParentData()
    //{
    //SqlConnection conn = new SqlConnection("server=192.168.3.81;uid=sa;pwd=;database=jmdata");
    //        SqlDataAdapter da = new SqlDataAdapter("select bmbh,bmmc from xt_bmzl", conn);
    //        DataSet ds = new DataSet();
    //        da.Fill(ds, "xt_bmzl");
    //        this.comboBox1.DataSource = ds.Tables["xt_bmzl"];
            
    //        this.comboBox1.DisplayMember = "bmmc"; //显示的列 
    //        this.comboBox1.ValueMember = "bmbh"; //数据列,次列会在selectedvalue中显示 
    //        //this.comboBox1.DisplayMember = comboBox1.Text;
    //        //this.comboBox1.Text =ds.Tables["xt_bmzl"].Rows[this.comboBox1 ]
    //}
}